/**
 * 
 */
package com.jie.eclipse.pan.gef.dialog;

import java.util.Iterator;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.jie.eclipse.pan.bind.BaseDataBind;
import com.jie.eclipse.pan.dao.QMDao;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.listener.PModifyListener;

/**
 * @author yanjie
 *
 */
public class PanconfigDialog extends BaseDialog{

	/**
	 * @param parentShell
	 * @param data
	 * @param action
	 */
	public PanconfigDialog(Shell parentShell, BaseDataEntity data, String action) {
		super(parentShell, data, action);
	}
	
	@Override
	protected Control createDialogArea(Composite paramComposite) {
		Color bgcolor = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		Composite composite = new Composite(paramComposite, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		composite.setBackground(bgcolor);
		Label lb = null;
		int j = 0;
		Iterator it = null;
				
		lb=  new Label(composite,SWT.RIGHT);
		lb.setText("当前系統");
		lb.setBackground(bgcolor);
		Combo sysName = new Combo(composite, SWT.READ_ONLY);
		sysName.setLayoutData(new GridData(200, 24));
		reloadTableName(sysName,QMDao.getInstance().getSysNameMap(true));
		this.getDataBind().bind("sysName", sysName);
		
		lb=  new Label(composite,SWT.RIGHT);
		lb.setText("当前表");
		lb.setBackground(bgcolor);
		Combo tablesn = new Combo(composite, SWT.READ_ONLY);
		tablesn.setLayoutData(new GridData(200, 24));
		reloadTableName(tablesn,QMDao.getInstance().getTableDataMap(""));
		this.getDataBind().bind("tablesn", tablesn);
		
		BaseDataEntity pdata = new BaseDataEntity();
		pdata.spov("tablesn", tablesn);
		sysName.addModifyListener(new PModifyListener(pdata){
			@Override
			public void modifyText(ModifyEvent paramModifyEvent) {
				Combo sysName = (Combo)paramModifyEvent.getSource();
				Combo tablesn = (Combo)this.data.gpov("tablesn");
				reloadTableName(tablesn,
						QMDao.getInstance().getTableDataMap(
								String.valueOf(sysName.getData(
										String.valueOf(sysName.getSelectionIndex())))));
			}
		});
		
		this.reloadRecord();
		return paramComposite;
	}
	
	@Override
	public void fillPanConfig(BaseDataEntity config, BaseDataBind dataBind) {
		dataBind.getRecord().CopyTo(QMDao.getInstance().getPanConfig(), false);
	}
	
	@Override
	public String getTitle() {
		return "小助手基本配置";
	}
	
	@Override
	protected Point getInitialSize() {
		Point point = super.getInitialSize();
		point.x = 400;
		return point;
	}
	
	public void reloadTableName(Combo combo,Map map){
		int j = 0;
		Iterator it = null;
		combo.removeAll();
		
		combo.setItems((String[])map.keySet().toArray(new String[0]));
		combo.setLayoutData(new GridData(200, 24));
		it = map.keySet().iterator();
		j = 0;
		while(it.hasNext()){
			String key = (String)it.next();
			combo.setData(String.valueOf(j), map.get(key));
			j++;
		}
	}

}
